load('GreenRelabelling.mat');
EventFirms=[{'Merlin'};{'Gecina'};{'Colonial'};{'Covivio'};{'EHC'};{'PSP'}];
EventDates=[datenum(2022,4,25) datenum(2021,4,14) datenum(2021,12,22) datenum(2022,5,19) datenum(2022,10,23) datenum(2022,11,8)];

EquityNo=[10 11 12; 21 19 20;10 11 12; 10 11 12; 6 7 8;1 2 3]; %First=Firm Equity price,Second=Local stock index,Third=Local Treasury index
NoFirms=length(EventFirms);


%%%%%%%%%%%%%%%%
%%Equity returns
DaysBefore=3;DaysAfter=3;
DaysTotal=DaysBefore+1+DaysAfter;
RelDays=-DaysBefore:1:DaysAfter;
BetaDays=29;
ret=zeros(NoFirms,DaysTotal);
exret=zeros(NoFirms,DaysTotal);
BondNumber=zeros(NoFirms,1);
AmountIssued=zeros(NoFirms,1);


Sharesoutstanding=[468.23 73.80 531.4 93.83 80.74 45.87]; %extracted 4-july 2023 from Eikon

for i=1:NoFirms
    edate=EventDates(i);
    eval(strcat('dat=',EventFirms{i},'.dat;'));
    datefirm=dat(:,2*EquityNo(i,1)-1);
    dateindex=dat(:,2*EquityNo(i,2)-1);
    [Lia Locb]=ismember(datefirm,dateindex); %extract dates where there is both firm and local equity prices
    dat2=[datefirm(Lia) dat(Lia,2*EquityNo(i,1)) dat(Locb(Locb>0),2*EquityNo(i,2))]; %1=date, 2=firm equity price, 3=local equity index   
    dat3=dat2(dat2(:,1)>0,:);
    dat4=dat3;
    dat4(:,1)=x2mdate(dat3(:,1));
    dat4(2:end,2:3)=dat3(2:end,2:3)./dat3(1:end-1,2:3)-1; %raw returns
    if i==5 %EHC dividend
        exdivdate=min(find(dat4(:,1)>=datenum('08Sep2022')));
        if isempty(exdivdate)==0
            dat4(exdivdate,2)=(dat3(exdivdate,2)+0.11)/dat3(exdivdate-1,2)-1;
        else end
    else end
    if i==2 %Gecina dividend
        exdivdate=min(find(dat4(:,1)>=datenum('03March2021')));
        if isempty(exdivdate)==0
            dat4(exdivdate,2)=(dat3(exdivdate,2)+2.65)/dat3(exdivdate-1,2)-1;
        else end
    else end
    if i==2 %Covivio dividend
        exdivdate=min(find(dat4(:,1)>=datenum('26April2021')));
        if isempty(exdivdate)==0
            dat4(exdivdate,2)=(dat3(exdivdate,2)+3.6)/dat3(exdivdate-1,2)-1;
        else end
    else end
    dat4(1,2:3)=NaN;
    reldates=dat4(:,1)-EventDates(i);
    zerodate=min(find(reldates(:,1)>=0));
    E(i)=Sharesoutstanding(i)*dat3(zerodate-1,2);
    BetaData=dat4((zerodate-BetaDays-DaysBefore):(zerodate-1-DaysBefore),:);
    y=BetaData(:,2);
    X=[ones(BetaDays,1) BetaData(:,3)];
    b = regress(y,X);
    tmp=[dat4((zerodate-DaysBefore):(zerodate+DaysAfter),2)'-(b(1)+b(2)*dat4((zerodate-DaysBefore):(zerodate+DaysAfter),3))'];
    exret(i,:)=cumsum(tmp);
    exret(i,:)=tmp;
end

%BONDS 
exretbond=zeros(NoFirms,DaysTotal);
for i=1:NoFirms
    edate=EventDates(i);
    eval(strcat('dat=',EventFirms{i},'.dat;'));
    eval(strcat('IssueAmount=',EventFirms{i},'.IssueAmount;'));
    exceldates=dat(:,2*EquityNo(i,3)-1);   
    exceldates2=exceldates(exceldates>0);
    dat2=dat(exceldates>0,:);
    dim=size(dat2);
    dat3=zeros(dim);
    for u=1:2:dim(2)
        [Lia Locb]=ismember(dat2(:,u),exceldates2);
        dat3(Locb(Locb>0),[u u+1])=dat2(Lia,[u u+1]);
    end
    matlabdates=x2mdate(exceldates2);
    reldates=matlabdates-EventDates(i);
    zerodate=min(find(reldates(:,1)>=0));
    allind=1:(length(dat3(1,:))/2);
    allbonds=[EquityNo(i,3) setdiff(allind,EquityNo(i,:))];
    dat4=dat3(:,2*allbonds);
    dat4(2:end,:)=dat4(2:end,:)./dat4(1:end-1,:);
    dat4(1,:)=NaN;
    DaysWithPrices=[zerodate-BetaDays-DaysBefore zerodate-1 zerodate zerodate+1 zerodate+DaysAfter];
    ind_bondsatevent=zeros(1,length(dat4(1,:)));
    for j=1:length(DaysWithPrices)
        ind_bondsatevent=ind_bondsatevent+dat4(DaysWithPrices(j),:)>0;
    end
    indb=ind_bondsatevent>(length(DaysWithPrices)-0.5);
    dat5=dat4(:,ind_bondsatevent); 
    amo=IssueAmount(ind_bondsatevent(2:end));
    weights=amo/sum(amo);
    AmountIssued(i)=sum(amo);
    BondNumber(i)=length(amo);
    retbond=dat5(:,2:end)*weights; 
    retind=dat5(:,1);
    y=retbond((zerodate-BetaDays-DaysBefore):(zerodate-1-DaysBefore));
    x=retind((zerodate-BetaDays-DaysBefore):(zerodate-1-DaysBefore));
    ind=(isfinite(y)+isfinite(x))>1.5;
    y=y(ind); x=x(ind);
    X=[ones(length(x),1) x];
    b = regress(y,X);
    tmp=[retbond((zerodate-DaysBefore):(zerodate+DaysAfter))'-(b(1)+b(2)*retind((zerodate-DaysBefore):(zerodate+DaysAfter)))'];
    exretbond(i,:)=cumsum(tmp);
    exretbond(i,:)=tmp;
end

